package com.chat.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chat.admin.domain.DTO.UserAppealDTO;
import com.chat.admin.domain.VO.UserAppealVO;
import com.chat.admin.mapper.UserAppealMapper;
import com.chat.admin.service.IUserAppealService;
import com.chat.common.common.constant.MessageConstant;
import com.chat.common.common.exception.RoutineException;
import com.chat.common.entity.UserAppeal;
import com.chat.common.form.ListForm;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author weikjie
 * @since 2024-05-11
 */
@Service
public class UserAppealServiceImpl extends ServiceImpl<UserAppealMapper, UserAppeal> implements IUserAppealService {

    @Override
    public ListForm<UserAppealVO> getUserApplication(UserAppealDTO userAppealDTO) {
        QueryWrapper<UserAppeal> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(userAppealDTO.getAccount())) {
            queryWrapper.eq("account", userAppealDTO.getAccount());
        }
        if (userAppealDTO.getProcessingResult() != null) {
            queryWrapper.eq("processing_results", userAppealDTO.getProcessingResult());
        }
        Page<UserAppeal> userAppealPage = new Page<>(userAppealDTO.getPage(), userAppealDTO.getSize());
        IPage<UserAppeal> userAppealIPage = page(userAppealPage, queryWrapper);
        List<UserAppeal> userAppealList = userAppealIPage.getRecords();
        List<UserAppealVO> userAppealListVO = userAppealList.stream()
                .map(userAppeal -> {
                    UserAppealVO userAppealVO = new UserAppealVO();
                    BeanUtil.copyProperties(userAppeal, userAppealVO);
                    return userAppealVO;
                }).toList();
        return new ListForm<>(userAppealDTO.getPage(), userAppealDTO.getSize(), userAppealIPage.getPages(),userAppealIPage.getTotal(),userAppealListVO);
    }

    @Override
    public UserAppeal getUserAppeal(String ID) {
//        UserAppeal userAppeal = baseMapper
//                .selectOne(
//                        new LambdaQueryWrapper<UserAppeal>()
//                                .eq(UserAppeal::getId,ID));
        UserAppeal userAppeal = baseMapper.selectById(ID);
        if(userAppeal == null) {
            throw new RoutineException("用户申请" + MessageConstant.ID_ERROR);
        }
        return userAppeal;
    }

    @Override
    public Boolean refuseUserAppeal(String ID) {
        UserAppeal userAppeal = getUserAppeal(ID);
        if(userAppeal.getProcessingResults() != 0 ){
            throw new RoutineException("当前申请已经处理过");
        }
        UpdateWrapper<UserAppeal> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", ID);
        // 设置处理时间
        updateWrapper.set("processing_time", LocalDateTime.now());
        // 设置 processing_results 属性为 2
        updateWrapper.set("processing_results", 2);
        // 执行更新操作
        return update(updateWrapper);
    }

    @Override
    public Boolean agreeUserAppeal(String ID) {
        UserAppeal userAppeal = getUserAppeal(ID);
        if(userAppeal.getProcessingResults() != 0 ){
            throw new RoutineException("当前申请已经处理过");
        }
        UpdateWrapper<UserAppeal> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", ID);
        // 设置处理时间
        updateWrapper.set("processing_time", LocalDateTime.now());
        // 设置 processing_results 属性为 1
        updateWrapper.set("processing_results", 1);
        // 执行更新操作
        return update(updateWrapper);
    }
}
